package org.galio.tourcoruna;



import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivityTour extends Activity 
{
	private LocationManager mlocationManager;
	private LocationListener mlocationListener;
	Thread thread;
	ProgressDialog pd;

	TipoLugar tipoLugar = TipoLugar.ALL;

	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_main);

		TextView museos = (TextView)this.findViewById(R.id.textViewMuseos);
		TextView monumentos = (TextView)this.findViewById(R.id.textViewMonumentos);
		TextView centros = (TextView)this.findViewById(R.id.textViewCentrosComerciales);
		TextView hoteles = (TextView)this.findViewById(R.id.textViewHoteles);
		TextView todo = (TextView)this.findViewById(R.id.textViewTodo);
		TextView tour = (TextView)this.findViewById(R.id.textViewtour);

		Typeface typeFace = Typeface.createFromAsset(getAssets(), "Catull.ttf");
		museos.setTypeface(typeFace);
		monumentos.setTypeface(typeFace);
		centros.setTypeface(typeFace);
		hoteles.setTypeface(typeFace);
		todo.setTypeface(typeFace);
		tour.setTypeface(typeFace);

		centros.setTextColor(Color.parseColor("#3369E8")); 
		hoteles.setTextColor(Color.parseColor("#D50F25"));
		monumentos.setTextColor(Color.parseColor("#EEB211"));
		museos.setTextColor(Color.parseColor("#009925"));     


		todo.setTextColor( Color.parseColor("#666666")); 


		//3369E8   D50F25  EEB211 009925

	}

	
	public void selectTypeView(){
		final String[] items = {"Ver por lista", "Ver por mapa"};
		 
	    AlertDialog.Builder builder = new AlertDialog.Builder(this);		 
	    builder.setTitle(tipoLugar.toString());
	    builder.setItems(items, new DialogInterface.OnClickListener() 
	    {
	        public void onClick(DialogInterface dialog, int item)
	        {
	            Log.i("Dialogos", "Opción elegida: " + items[item]);
	             if(items[item].equals("Ver por mapa"))
	               {
	            	getPosicion();
	               }
	             else if(items[item].equals("Ver por lista"))  {
	            	 mostrarLista();
		            }
	             
	        }
	    });
	    builder.create();
	    builder.show();
	}
	
	public void RHoteles(View view)
	{
		tipoLugar = TipoLugar.HOTEL;
		selectTypeView();
	}

	public void RMonumentos(View view)
	{
		tipoLugar = TipoLugar.MONUMENTO;
		selectTypeView();
	}

	public void RCentrosComerciales(View view)
	{
		tipoLugar = TipoLugar.CCOMERCIAL;
		selectTypeView();
	}

	public void RMuseos(View view)
	{
		tipoLugar = TipoLugar.MUSEO;
		selectTypeView();
	}

	public void RTodo(View view)
	{
		tipoLugar = TipoLugar.ALL;
		selectTypeView();
	}


	public void getPosicion()
	{

		thread = new Thread(){
			@Override
			public void run()
			{
				Looper.prepare(); 
				pd = new ProgressDialog(MainActivityTour.this);
				pd.setMessage("CARGANDO DATOS");
				pd.setCancelable(true);
				pd.show();

				mlocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
				mlocationListener = new MyLocationListener();

				if (mlocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) )
				{
					mlocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 15, 15, mlocationListener);
				}
				else if (mlocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) )
				{	
					mlocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15, 15, mlocationListener);
				}
				else   
				{
					pd.dismiss();
					Toast.makeText(MainActivityTour.this, "Redes inalambricas y GPS desactivados.", Toast.LENGTH_LONG).show();
				}


				Looper.loop();
			}

		};
		thread.start();
	}

	@Override
	public void onBackPressed() 
	{
		super.onBackPressed();
		System.exit(0);  
	}



	@Override
	protected void onStop() 
	{
		super.onStop();
		if(pd!=null)
		{
		pd.dismiss();
		if (mlocationManager!=null)
			mlocationManager.removeUpdates(mlocationListener);
		mlocationManager = null;
		}
	}




	private class MyLocationListener implements LocationListener //interfaz
	{

		public void onLocationChanged(Location location) 
		{

			mostrarPosicion(location);			

		}

		public void onProviderDisabled(String provider) {}

		public void onProviderEnabled(String provider) {}

		public void onStatusChanged(String provider, int status, Bundle extras) {}


	}

	public void mostrarPosicion(Location location)
	{
		if (location != null) 
		{	

			Bundle bundle = new Bundle();

			bundle.putDouble("miLat", location.getLatitude());
			bundle.putDouble("miLon", location.getLongitude());
			bundle.putInt("tipoLugar", tipoLugar.getNum());

			Intent i = new Intent(MainActivityTour.this, MiMapaParadaTour.class);
			i.putExtras(bundle);	
			startActivity(i);


		}

		else Toast.makeText(this, "LOCALIZACIÓN NULA", Toast.LENGTH_SHORT).show();



	}  
	
	public void mostrarLista (){
		Bundle bundle = new Bundle();
		bundle.putInt("tipoLugar", tipoLugar.getNum());
		Intent intent = new Intent(MainActivityTour.this, MiListaTour.class);
		intent.putExtras(bundle);	
		startActivity(intent);
	}

}
